﻿using System;
using System.Text;

namespace SharpScript
{
    public static class StringHelpers
    {
        /// <summary>
        /// Removes an exemplar string from the last occuring matching.
        /// </summary>
        /// <param name="source">The string to be trimmed.</param>
        /// <param name="exemplar">The string to trim off.</param>
        /// <param name="trimLastOccurance"></param>
        /// <returns></returns>
        public static String Remove(this String source, String exemplar, Boolean trimLastOccurance)
        {
            if (String.IsNullOrEmpty(source) || String.IsNullOrEmpty(exemplar)) return source;

            var result = new StringBuilder(source);
            var count = exemplar.Length;
            var startIndex = trimLastOccurance ? source.LastIndexOf(exemplar) : source.IndexOf(exemplar);
            return result.Remove(startIndex, count).ToString();
        }
    }
}
